package BackgammonBot.Algorithmus;

import java.util.ArrayList;

import AIColorUSAStates.Graph.FirstColorType;
import BackgammonBot.GameLogic.BackgammonLogic;

public class AlphaBetaAlgoThreadStart
{
	AlphaBetaAlgoMultiThread algoThreadFirst;
	AlphaBetaAlgoMultiThread algoThreadSecond;
	AlphaBetaAlgo algo;
	
	public void run(int depth, byte[] board
    		, byte whiteCheckersInHome
    		, byte whiteCheckersAtStart
    		, byte blackCheckersInHome
    		, byte blackCheckersAtStart
    		, char initialPlayer
    		, int[] initialDice)
	{
		int processors = Runtime.getRuntime().availableProcessors();
		
		if (processors >= 2 && initialDice[1] != initialDice[0])
		{
			
			int[] diceThrow = new int[2];
			diceThrow[0] = initialDice[1];
			diceThrow[1] = initialDice[0];
			
			BackgammonLogic gammonFirst = new BackgammonLogic(board
							, (byte) whiteCheckersInHome
							, (byte) whiteCheckersAtStart
							, (byte) blackCheckersInHome
							, (byte) blackCheckersAtStart
							, initialPlayer
							, initialDice);
			
			BackgammonLogic gammonSecond = new BackgammonLogic(board
					, (byte) whiteCheckersInHome
					, (byte) whiteCheckersAtStart
					, (byte) blackCheckersInHome
					, (byte) blackCheckersAtStart
					, initialPlayer
					, diceThrow);
			
			algoThreadFirst = new AlphaBetaAlgoMultiThread(depth, gammonFirst);
			algoThreadSecond = new AlphaBetaAlgoMultiThread(depth, gammonSecond);
			
			algoThreadFirst.start();
			algoThreadSecond.start();
			
			try 
			{
				algoThreadFirst.join();
				algoThreadSecond.join();
			} catch (InterruptedException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			BackgammonLogic gammonLogic = new BackgammonLogic(board
					, (byte) whiteCheckersInHome
					, (byte) whiteCheckersAtStart
					, (byte) blackCheckersInHome
					, (byte) blackCheckersAtStart
					, initialPlayer
					, initialDice);
			
			algo = new AlphaBetaAlgo(depth, gammonLogic);
		}
	}
	
	public ArrayList<int[]> getMove() 
	{
		if (algo != null && algoThreadFirst.getMoveFittness() > algoThreadSecond.getMoveFittness())
			return algoThreadFirst.getMove();
		else if (algo != null && algoThreadSecond.getMoveFittness() > algoThreadFirst.getMoveFittness())
			return algoThreadSecond.getMove();
		else
			return algo.getMove();
	}
}
